
/**
 * AppleIIGo
 * Apple 5.25" Disk Drive class
 * (C) 2008 by Marc S. Ressl (mressl@umich.edu)
 * Released under the GPL
 */

#include "Apple525DiskDrive.h"

Apple525DiskDrive::Apple525DiskDrive() :
FloppyDiskDrive(DISKDRIVETYPE_5_25) {
	zeroBitCount = 0;
}

void Apple525DiskDrive::onPhysicalPhaseUpdate() {
	isWriteEnabled = diskImage->isWriteEnabled();
	isDiskWriteProtected = diskImage->isWriteProtected();
	if (getBit(phaseRegister, PHASE1))
		isWriteEnabled = false;
	
	SINT32 trackIndex = diskImage->getTrackIndex();
	UINT32 trackNum = diskImage->getTrackNum();
	UINT32 currentPhase = trackIndex & 0x7;
	UINT32 nextPhase;
	
	switch (phaseRegister) {
		case 0x1: case 0xb:
			nextPhase = 0;
			break;
		case 0x3:
			nextPhase = 1;
			break;
		case 0x2: case 0x7:
			nextPhase = 2;
			break;
		case 0x6:
			nextPhase = 3;
			break;
		case 0x4: case 0xe:
			nextPhase = 4;
			break;
		case 0xc:
			nextPhase = 5;
			break;
		case 0x8: case 0xd:
			nextPhase = 6;
			break;
		case 0x9:
			nextPhase = 7;
			break;
		case 0x0: case 0x5: case 0xa: case 0xf:
			nextPhase = currentPhase;
			break;
	}
	
	SINT32 phaseDifference = ((nextPhase - currentPhase + 4) & 0x7) - 4;
	trackIndex += phaseDifference;
	
	if (trackIndex < 0) {
		trackIndex = 0;
		// to-do: stopper sound
	} else if (trackIndex >= trackNum) {
		trackIndex = trackNum - 1;
		// to-do: stopper sound
	}
	
	diskImage->setTrackIndex(trackIndex);
	
	updateDiskTrack();
}

bool Apple525DiskDrive::senseInput() {
	return isDiskWriteProtected;
}
